package ar.edu.unicen.exa.isistan.archassis.ia.seed;

import java.io.Serializable;

import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;



public class VariableSeed implements ISeed, Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private String type;
	private String visibility;
	//private String isStatic;
	private boolean _isStatic;
	private boolean _isFinal;
	private float peso = 1;
	
	
	public String getName() {
		return name;
	}

	public float getPeso() {
		return peso;
	}

	public void setPeso(float peso) {
		this.peso = peso;
	}

	public VariableSeed(String type, String visibility, String isStatic, String isFinal, String name) {
		this.type = type;
		this.visibility = visibility;
		this._isStatic = parseValue(isStatic);
		this._isFinal = parseValue(isFinal);
		this.name = name;
	}
	private static boolean parseValue(String s) {
		if (s == null) return false;
		if (s.trim().equals("")) return false;
		return true;
	}

	@Override
	public float eval(JavaClass c) {
		int pos = c.getVariables().indexOf(this);
		if (pos != -1){
			VariableSeed var = c.getVariables().get(pos);
			float result = 0.7f;
			if (visibility.equals(var.getVisibility())) result += 0.1f;
			if ( this._isStatic == var.isStatic() ) result += 0.1f;
			if ( this._isFinal == var.isFinal() ) result += 0.1f;
			return (result * peso);
		}
		return 0;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof VariableSeed){
			return type.equals(((VariableSeed) obj).getType());
		}
		return false;
	}

	public String getType() {
		return type;
	}

	public String getVisibility() {
		return visibility;
	}

	public boolean isStatic() {
		return _isStatic;
	}

	public boolean isFinal() {
		return _isFinal;
	}
	
	public String getAttr(){
		StringBuffer attr = new StringBuffer(visibility);
		if (isStatic()) {
			attr.append("Static");
		}
		if (isFinal()) {
			attr.append("Final");
		}
		//if (!"".equals(this.isStatic))
		//	attr += "Static";
		//if (!"".equals(this.isFinal))
		//	attr += "Final";
		return attr.toString();
	}

	@Override
	public void updatePeso(JavaClass c) {
		int pos = c.getVariables().indexOf(this);
		if (pos != -1){
			VariableSeed var = c.getVariables().get(pos);
			float result = 0.7f;
			if (visibility.equals(var.getVisibility())) result += 0.1f;
			if ( this._isStatic == var.isStatic() ) result += 0.1f;
			if ( this._isFinal == var.isFinal() ) result += 0.1f;
			peso = result * peso;
			//var.setPeso(var.getPeso() * result);
		}
		
	}

	
}
